package com.chinatelecom.epush.async;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.chinatelecom.epush.R;
import com.chinatelecom.epush.net.ApacheHttpClient;
import com.chinatelecom.epush.net.ClientRequestedURL;

/**
 * 登录异步类
 * 
 * @author admin
 * 
 */
public class LoginAsyncTask extends AsyncTask<String, Void, List<Object>> {

	public static final int TYPE_SEND_MOBILE = 1;// 发送电话号码
	public static final int TYPE_SEND_IDENTIFY = 2;// 发送验证码
	public static final int TYPE_VALIDATE_IDENTIFY = 3;// 验证唯一标识码是否失效

	private Context mContext;
	private Handler mHandler;
	private ProgressDialog mProgressDialog;

	private int mType;

	private String url;
	private String strResponse;
	private JSONObject jsonResponse;
	private String flag;
	private String loginsign;
	private String userInfo;
	private String errorMsg;

	private ApacheHttpClient mClient;

	public LoginAsyncTask(Context context, Handler handler, int type) {
		this.mContext = context;
		this.mHandler = handler;
		this.mType = type;
		this.mClient = new ApacheHttpClient();
		setProgressStyle(this.mType);
	}

	/**
	 * 加载进度框设置
	 */
	private void setProgressStyle(int type) {
		if (type != TYPE_VALIDATE_IDENTIFY) {
			mProgressDialog = new ProgressDialog(mContext);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.setCancelable(true);
			switch (type) {
			case TYPE_SEND_MOBILE:
				mProgressDialog.setTitle(R.string.data_loading_title);
				mProgressDialog.setMessage(mContext
						.getString(R.string.send_mobile_phone_wait));
				break;
			case TYPE_SEND_IDENTIFY:
				mProgressDialog.setTitle(R.string.data_loading_title);
				mProgressDialog.setMessage(mContext
						.getString(R.string.send_identify_wait));
				break;
			default:
				break;
			}
		}
	}

	@Override
	protected List<Object> doInBackground(String... params) {

		List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
		switch (mType) {
		case TYPE_SEND_MOBILE:
			//url = null;
			paramsList.add(new BasicNameValuePair("mobile", params[0]));
			try {
				strResponse = mClient.httpPost(url, paramsList);
				jsonResponse = new JSONObject(strResponse);
				flag = jsonResponse.optString("flag", "0");
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case TYPE_SEND_IDENTIFY:
			//url = null
			paramsList.add(new BasicNameValuePair("mobile", params[0]));
			paramsList.add(new BasicNameValuePair("code", params[1]));
			paramsList.add(new BasicNameValuePair("devtype", "ayzc"));
			try {
				strResponse = mClient.httpPost(url, paramsList);
				jsonResponse = new JSONObject(strResponse);
				flag = jsonResponse.optString("flag", "0");
				errorMsg = jsonResponse.optString("erromsg", "");
				loginsign = jsonResponse.optString("loginsign", "");
				userInfo = jsonResponse.optString("user", "");
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case TYPE_VALIDATE_IDENTIFY:
			//url 
			paramsList.add(new BasicNameValuePair("loginsign", params[0]));
			try {
				strResponse = mClient.httpPost(url, paramsList);
				jsonResponse = new JSONObject(strResponse);
				flag = jsonResponse.optString("flag", "0");
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
		return null;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (this.mType == TYPE_SEND_MOBILE || this.mType == TYPE_SEND_IDENTIFY) {
			if (mProgressDialog != null) {
				mProgressDialog.show();
			}
		}
	}

	@Override
	protected void onPostExecute(List<Object> result) {
		super.onPostExecute(result);
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
		Message msg = Message.obtain();
		msg.what = this.mType;

		List<Object> list = new ArrayList<Object>();
		switch (this.mType) {
		case TYPE_SEND_MOBILE:
			if (flag != null) {
				list.add(flag);
				msg.obj = list;
			} else {
				msg.obj = null;
			}
			break;
		case TYPE_SEND_IDENTIFY:
			if (flag != null) {
				list.add(flag);
				list.add(errorMsg);
				list.add(loginsign);
				list.add(userInfo);
				msg.obj = list;
			} else {
				msg.obj = null;
			}
			break;
		case TYPE_VALIDATE_IDENTIFY:
			if (flag != null) {
				list.add(flag);
				msg.obj = list;
			} else {
				msg.obj = null;
			}
			break;

		default:
			break;
		}
		mHandler.sendMessage(msg);
	}
}